package se.webbzon.boltzmann.editor.component.appearance;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.vecmath.Vector3d;

import se.webbzon.boltzmann.editor.component.AbstractComponent;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;

public class SimpleAppearancePanel extends AbstractComponent {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = 2299338351035593568L;

	// The text field inputs in this panel
	private final JTextField 	xLayer, yLayer, zLayer, 
								renderingOrder, transparency,
								hOffset, vOffset, hRep, vRep, rotation;
			
	// The blending color chooser in this panel
	private final JColorChooser blending;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public SimpleAppearancePanel(SimpleAppearance3D appearance) {
		super("Appearance");
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		JPanel layer = new JPanel();
		layer.setBorder(BorderFactory.createTitledBorder("Layer"));
		layer.setLayout(new GridLayout(2,3));
		layer.add(new JLabel("x:"));
		layer.add(new JLabel("y:"));
		layer.add(new JLabel("z:"));
		
		boolean read = appearance.hasLayers();
		Vector3d layerVector = new Vector3d();
		
		if (read)
			appearance.getLayer(layerVector);
		xLayer = new JTextField("" + layerVector.getX());
		yLayer = new JTextField("" + layerVector.getY());
		zLayer = new JTextField("" + layerVector.getZ());
		
		layer.add(xLayer);
		layer.add(yLayer);
		layer.add(zLayer);
		add(layer);
		
		JPanel blending = new JPanel();
		blending.setBorder(BorderFactory.createTitledBorder("Blending"));
		blending.setLayout(new BoxLayout(blending,BoxLayout.Y_AXIS));
		this.blending = new JColorChooser(appearance.getColor());
		blending.add(this.blending);
		add(blending);
		
		JPanel transparencyPanel = new JPanel(); 
		transparencyPanel.setBorder(BorderFactory.createTitledBorder("Transparency"));
		transparencyPanel.setLayout(new GridLayout(2,2));
		transparencyPanel.add(new JLabel("Rendering Order:"));
		transparencyPanel.add(new JLabel("Transparency:"));
		renderingOrder = new JTextField("" + appearance.getRenderingOrder());
		transparencyPanel.add(renderingOrder);
		read = appearance.hasTransparency();
		transparency = new JTextField(read ? "" + appearance.getTransparency() : "0");
		transparencyPanel.add(transparency);
		add(transparencyPanel);
		
		
		JPanel transform = new JPanel();
		transform.setBorder(BorderFactory.createTitledBorder("Texture Transform"));
		transform.setLayout(new GridLayout(6,2));
		
		read = appearance.hasTextureTranslation();
		hOffset = new JTextField(read ? "" + appearance.getHOffset() : "0");
		vOffset = new JTextField(read ? "" + appearance.getVOffset() : "0");

		read = appearance.hasTextureRepeating();
		hRep = new JTextField(read ? "" + appearance.getHRep() : "1");
		vRep = new JTextField(read ? "" + appearance.getVRep() : "1");
		
		read = appearance.hasTextureRotation();
		rotation = new JTextField(read ? "" + appearance.getRotation() : "0");
		
		transform.add(new JLabel("Horizontal Offset:"));
		transform.add(new JLabel("Vertical Offset:"));
		transform.add(hOffset);
		transform.add(vOffset);
		transform.add(new JLabel("Horizontal Repeat:"));
		transform.add(new JLabel("Vertical Repeat:"));
		transform.add(hRep);
		transform.add(vRep);
		transform.add(new JLabel("Rotation (radians):"));
		transform.add(new JLabel(""));
		transform.add(rotation);
		add(transform);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the selected x-layer from this panel. **/
	public double getXLayer() throws NumberFormatException {
		return Double.parseDouble(xLayer.getText());
	}
	
	/** Returns the selected y-layer from this panel. **/
	public double getYLayer() throws NumberFormatException {
		return Double.parseDouble(yLayer.getText());
	}
	
	/** Returns the selected z-layer from this panel. **/
	public double getZLayer() throws NumberFormatException {
		return Double.parseDouble(zLayer.getText());
	}
	
	/** Returns the selected color from this panel. **/
	public Color getColor() {
		return blending.getColor();
	}
	
	/** Returns the selected rendering order from this panel. **/
	public int getRenderingOrder() throws NumberFormatException {
		return Integer.parseInt(renderingOrder.getText());
	}
	
	/** Returns the selected transparency from this panel. **/
	public float getTransparency() throws NumberFormatException {
		return Float.parseFloat(transparency.getText());
	}
	
	/** Returns the horizontal texture offset from this panel. **/
	public double getHOffset() throws NumberFormatException {
		return Double.parseDouble(hOffset.getText());
	}
	
	/** Returns the vertical texture offset from this panel. **/
	public double getVOffset() throws NumberFormatException {
		return Double.parseDouble(vOffset.getText());
	}
	
	/** Returns the horizontal texture repeating from this panel. **/
	public double getHRep() throws NumberFormatException {
		return Double.parseDouble(hRep.getText());
	}
	
	/** Returns the vertical texture repeating from this panel. **/
	public double getVRep() throws NumberFormatException {
		return Double.parseDouble(vRep.getText());
	}
	
	/** Returns the texture rotation from this panel. **/
	public double getRotation() throws NumberFormatException {
		return Double.parseDouble(rotation.getText());
	}

}
